package com.xiaoy.web;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.hibernate.HibernateUtils;

import com.xiaoy.page.PageWrapper;
import com.xiaoy.util.FreeMarkerUtil;
import com.xiaoy.util.page.FormInfo;
import com.xiaoy.util.page.PageParamUtil;
import com.xiaoy.util.store.Templatekeys;

import freemarker.template.Configuration;

public class BasicAction extends AsynAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected Logger logger = LoggerFactory.getLogger(getClass());

	
	protected PageWrapper pageBean;
	
	// 属性区域  -------end 
	
	// 业务区域 --------start
	
	// 业务区域 --------end
	
	protected Page InitPageWrapper (PageWrapper page) {
		pageBean = page;
		return initPageWrapper();
	}
	protected Page initPageWrapper () {
		if (pageBean == null) {
			pageBean = new PageWrapper();
		}
		
		pageBean.setOrderBy(getParameter("orderBy"));
		
		pageBean.setOrder(getParameter("order"));
		
		pageBean.setPageNo(getParameterInt("pageNo"));
		
		if (getParameterInt(Templatekeys.Pagination.PAGESIZE) > 1) {
			pageBean.setPageSize(getParameterInt(Templatekeys.Pagination.PAGESIZE));
		}
		List<PropertyFilter> filters = HibernateUtils.buildPropertyFilters(request);
		pageBean.setFilters(filters);
		return pageBean;
	}
	
	public String list () {
		return "list";
	}
	
	@Override
	public String execute() throws Exception {
		return list();
	}
	
	/**
	 * 得到审批记录
	 * @return
	 */
	public Map<String,Object> getApprovaRecord () {
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		// 审批记录的部分   String approval = VariableStore.getValue("page.approvalRecord");
		String[]keys = request.getParameterValues("approvalRecord");
		for (String key:keys) {
			String paramValue=getParameter(key);
			map.put(key, paramValue);
		}
	
		return map;
	}
	
	/**
	 * 得到表单信息
	 * @return
	 */
	public FormInfo getFormInfo () {
		
		return PageParamUtil.getFormInfo(request);
	}
	/**
	 * 
	 * @return
	 */
	public Map<String,Object> getFilterInfo () {
		
		return PageParamUtil.getFilterInfo(request);
	}
	
	/**
	 * 得到及时的用户
	 * @return
	 */
	public String getCurrentUser () {
		String userid = getParameter(Templatekeys.USERNAME);
		if (userid == null || "".equals(userid)) {
			Object obj = request.getAttribute(Templatekeys.USERNAME);
			if (obj == null) {
				obj = request.getSession().getAttribute(Templatekeys.USERNAME);
				if (obj!=null) {
					return obj.toString();
				}
			}else {
				return obj.toString();
			}
		}
		return null;
	}
	
	/**
	 * 得到登陆用户
	 * @return
	 */
	public String getLoginUser () {
		Object obj = request.getSession().getAttribute(Templatekeys.USERNAME);
		if (obj!=null) {
			return obj.toString();
		}
		return "admin";
	}
	/**
	 * 保存登陆用户
	 * @param username
	 */
	public void setLoginUser (String username) {
		request.getSession().setAttribute(Templatekeys.USERNAME, username);
	}
	
	public PageWrapper getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageWrapper pageBean) {
		this.pageBean = pageBean;
	}


	// 得到参数  辅助 ------start
	protected String[] getParameters(String key){
		return request.getParameterValues(key);
	}
	protected String getParameter(String key) {
		String r = request.getParameter(key);
		if(r==null) {
			return "";
		}
		return r;
	}
	
	protected long[] getParameterLongs(String key) {
		
		String keys[] = getParameters(key);
		long[]a = new long[keys.length];
		for (int i = 0; i <keys.length; i++) {
			a[i] = Long.parseLong(keys[i]);
		}
		return a;
	}
	protected long getParameterLong(String key) {
		
		try{
			return Long.parseLong(request.getParameter(key));
		}catch (Exception e) {
			//logger.error("接收long型参数异常", e);
			//throw new SystemException("地址注入！");
			return -1;
		}
	}
	
	protected int getParameterInt(String key) {
		
		try{
			return Integer.valueOf(request.getParameter(key));
		}catch (Exception e) {
			//logger.error("接收int型参数异常");
			return -1;
//			throw new SystemException("地址注入！");
		}
	}
	
	protected void setAttribute(String key,Object obj) {
		request.setAttribute(key, obj);
	}
	protected void getAttribute(String key) {
		request.getAttribute(key);
	}
	
	protected void setSessionAttribute(String key,Object obj) {
		session.setAttribute(key, obj);
	}
	protected Object getSessionAttribute(String key) {
		return session.getAttribute(key);
	}

	/**
	 * 取得带相同前缀的Request Parameters.
	 * 
	 * 返回的结果Parameter名已去除前缀.
	 */
	protected Map<String,Object> filterParametersPrefix(String prefix) {
		
		Enumeration paramNames = request.getParameterNames();
		Map<String,Object>params = new TreeMap<String,Object>();
		if (prefix == null) {
			prefix = "";
		}
		while (paramNames != null && paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			if ("".equals(prefix) || paramName.startsWith(prefix)) {
				String unprefixed = paramName.substring(prefix.length());
				String[] values = request.getParameterValues(paramName);
				
				if (values == null || values.length == 0) {// NOSONAR
					// Do nothing, no values found at all.
				} else if (values.length > 1) {
					params.put(unprefixed, values);
				} else {
					params.put(unprefixed, values[0]);
				}
			}
		}
		return params;
	}
	
	// 得到参数  辅助 ------end
	//-----------------------------------------------
	protected Configuration freeMarkerCfg =  FreeMarkerUtil.initFreeMarker(session);
	
	
	
	
	
}
